package cn.gaozhong.ymcc.service.impl;

import cn.gaozhong.ymcc.domain.CourseType;
import cn.gaozhong.ymcc.dto.CourseMbxDto;
import cn.gaozhong.ymcc.mapper.CourseTypeMapper;
import cn.gaozhong.ymcc.service.ICourseTypeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author GZ.gaozhong
 * @since 2022-07-20
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    @Cacheable(cacheNames={"courseTypeTree"},key="'tree'")
    public List<CourseType> treeData() {
        List<CourseType> tree = tree();
        return tree;

    }



    public List<CourseType> treeData2() {
        String str = "courseTypeTree";
        Object o = redisTemplate.opsForValue().get(str);
        if (o!=null){
            return (List<CourseType>)o;
        }
        List<CourseType> tree = tree();
        redisTemplate.opsForValue().set(str,tree);
        return tree;

    }
    public List<CourseType> tree() {
        System.out.println("走sql");
        List<CourseType> courseTypes = selectList(null);
        List<CourseType> firstList = new ArrayList<>();
        for (CourseType type:courseTypes){
            if (type.getPid()==null||type.getPid().intValue()==0){
                firstList.add(type);
            }else {
                for (CourseType parent:courseTypes){
                    if (parent.getId().intValue()==type.getPid().intValue()){
                        parent.getChildren().add(type);
                    }
                }
            }
        }
        return firstList;
    }

    @Override
    @CacheEvict(cacheNames="courseTypeTree", key = "'tree'")
    public boolean insert(CourseType entity) {
        boolean insert = super.insert(entity);
        return insert;
    }

    @Override
    @CacheEvict(cacheNames="courseTypeTree", key = "'tree'")
    public boolean deleteById(Serializable id) {
        boolean deleteById = super.deleteById(id);
        return deleteById;
    }

    @Override
    @CacheEvict(cacheNames="courseTypeTree", key = "'tree'")
    public boolean updateById(CourseType entity) {
        boolean updateById = super.updateById(entity);
        return updateById;
    }

    @Override
    public List<CourseMbxDto> crumbs(Long courseTypeId) {
        CourseType type = selectById(courseTypeId);
        String[] split = type.getPath().split(",");
        List<CourseMbxDto> courseMbxDtos = new ArrayList<>();
        for (String s : split) {
            CourseMbxDto courseMbxDto = new CourseMbxDto();
            CourseType selectById = selectById(s);
            Wrapper<CourseType> wrapper = new EntityWrapper<>();
            wrapper.eq("pid",selectById.getPid());
            List<CourseType> courseTypes = selectList(wrapper);
            courseMbxDto.setOwnerProductType(selectById);
            courseMbxDto.setOtherProductTypes(courseTypes);
            courseMbxDtos.add(courseMbxDto);
        }
        return courseMbxDtos;
    }
}
